/**
 * @file task_system.cpp
 * @brief Disk management
 *           EE file system management
 *           Power management
 */

#include "task_system.h"

#include "FreeRTOS.h"
#include "timers.h"
#include "ff.h"
#include "diskio.h"

#include "ee_driver.h"
#include "led_driver.h"
#include "eefs.h"
#include "ee_driver.h"
#include "stdlib.h"
#include "string.h"

#include "task_mix.h"


FATFS fatFs;
int fsmDisk =0;
static int fsm_ee=0;
EeFs *eefs;
TransmiterFile transmiterFile;
ModelFile modelFile;
char transmiterFileDirty;
char modelFileDirty;
xSemaphoreHandle fileSemaHandle;
unsigned char modelId;

static xTimerHandle disk_check_timer;

/// check disk status
void disk_check( xTimerHandle timerHandle)
{
    disk_timerproc();
}

void test_eefs()
{
    EeFile transmiter;
    int i, t_int;
    char buffer[]="0123231222222222222222223333333333331111111111122222222222";///"0a0a00aa00aa00aa000aaa000aaa0000aaaa0000aaaa00000aaaaa00000aa0a0000000000000000000000a";

        for( i=0; i<64; i++)
        {
            t_int =eefs->open(&transmiter, i, EE_MODE_WRITE);
            eefs->writeRLE(&transmiter, buffer, sizeof(buffer), 0);
            eefs->close(&transmiter);
        }

        for( i=0; i<64; i++)
        {
           char t_char;
           //t_char ^=1;
           //led_driver.write(&led_driver, &t_char, 1, 0);
           //memset(buffer, 0, sizeof(buffer));

           t_int =eefs->open(&transmiter, 0, EE_MODE_READ);
           eefs->readRLE(&transmiter, buffer, sizeof(buffer),0);
           eefs->close(&transmiter);

           if(strcmp(buffer, "0123231222222222222222223333333333331111111111122222222222") )
           {
                while (1);
           }
       }

       while(1);
}

void taskSystem(void *pData)
{
    FIL t_file;

    /// mount a timer and 
    disk_check_timer =xTimerCreate((const signed char *)"check disk", 10/portTICK_RATE_MS, pdTRUE, (void *)0, disk_check);
    if(disk_check_timer)
    {
        xTimerStart( disk_check_timer,  0);
    }

    xTaskCreate(taskMix, (const signed char *)"taskMix", TASK_MIX_STACK_SIZE, NULL, 3, NULL);

    while(1)
    {
        vTaskDelay(500/portTICK_RATE_MS);    ///< delay 0.5S

        //disk management
        if(1)
        {
            switch(fsmDisk)
            {
            case 0:
                if(f_mount(0, &fatFs) == FR_OK)  fsmDisk =1;
                else break;
            case 1:
                if(disk_initialize(0) == FR_OK) fsmDisk =2;
                else break;
            case 2:
                //if(f_open(&t_file, "BMP/Alpha.bmp", FA_READ) ==FR_OK) fsmDisk =3;
                break;
            case 3:
                break;
            case 4:
                break;
            }
        }

        //ee management;
        if(transmiterFileDirty || modelFileDirty)
        {
            int err;
            EeFile ee_file;

            TransmiterFile t_trans;
            ModelFile t_model;

            FILE_LOCK();
            memcpy(&t_trans, &transmiterFile, sizeof(transmiterFile) );
            FILE_UNLOCK();
            
            if(transmiterFileDirty)
            {
                eefs->open(&ee_file, FILE_ID_TRANSMITER, EE_MODE_WRITE);
                eefs->writeRLE(&ee_file, &t_trans, sizeof(transmiterFile), 0);
                eefs->close(&ee_file);

                eefs->open(&ee_file, transmiterFile.currModel, EE_MODE_READ);
                eefs->readRLE(&ee_file, &t_model, sizeof(modelFile), 0);
                eefs->close(&ee_file);

                FILE_LOCK();
                memcpy(&modelFile, &t_model, sizeof(modelFile) );
                FILE_UNLOCK();
                
                transmiterFileDirty =0;
                modelFileDirty =0;
            }

            if(modelFileDirty)
            {
                FILE_LOCK();
                memcpy(&t_model, &modelFile, sizeof(modelFile) );
                FILE_UNLOCK();
                eefs->open(&ee_file, transmiterFile.currModel, EE_MODE_WRITE);
                eefs->writeRLE(&ee_file, &t_model, sizeof(modelFile), 0);
                eefs->close(&ee_file);
                modelFileDirty =0;
            }
        }

        // power management
        if(1)
        {
        }
    }
}



